package com.shujia.day10;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class Demo07MapTest {
    public static void main(String[] args) {

        TreeMap<Character, Integer> treeMap = new TreeMap<>();
        String str = "aababcabcdabcde";
        for (char c : str.toCharArray()) {
            /*
                累加的逻辑：
                    当TreeMap中没有出现 某个字符，当出现时那么给定一个初始值 1
                        如果已经存在有某个字符， 可以对当前的值进行加1
             */
            if (!treeMap.containsKey(c)) {
                treeMap.put(c,1);
            }else {
                Integer value = treeMap.get(c);
                value += 1;
                treeMap.put(c,value);
//                treeMap.get(c) += 1; // 报错

            }
        }
        System.out.println(treeMap);

//        TreeMap<String, String> strTreeMap = new TreeMap<>();
//        strTreeMap.put("1","1");
//        strTreeMap.get("1") += "1";

        HashMap<String, HashMap<String, String>> classStu = new HashMap<>();
        HashMap<String, String> oneClazzStu = new HashMap<>();
        oneClazzStu.put("001","汪雨");
        oneClazzStu.put("002","杨德广");
        classStu.put("bigdata30",oneClazzStu);

        HashMap<String, String> oneClazzStu2 = new HashMap<>();
        oneClazzStu2.put("001","老邢");
        oneClazzStu2.put("002","老徐");
        classStu.put("bigdata1",oneClazzStu2);


//        for (String clazz : classStu.keySet()) {
//            for (Map.Entry<String, String> entry : classStu.get(clazz).entrySet()) {
//                System.out.println(entry);
//            }
//        }

        // BiConsumer<? super K, ? super V> action
        classStu.forEach(
                new BiConsumer<String, HashMap<String, String>>() {
                    @Override
                    public void accept(String clazz, HashMap<String, String> value) {
                        value.forEach(new BiConsumer<String, String>() {
                            @Override
                            public void accept(String key, String value) {
                                System.out.println(clazz+"\t"+key+"\t"+value);
                            }
                        });
                    }
                }
        );

        // HashMap和 Hashtable 的区别
        //   方法基本一致，但是Hashtable是线程不安全的，效率高，当获取到迭代器后，对结构进行改变后，会引发
        //     ConcurrentModificationException


    }
}
